﻿using System;
using System.Linq;
using ProjetoComperio.DataLayer.Entities;
using ProjetoComperio.DataLayer.Infra;
using ProjetoComperio.Framework.Extensions;

namespace ProjetoComperio.BusinessLogicLayer
{
    public class ProposalBLL : ComperioDataObject
    {
        /// <summary>
        /// This method inserts a new proposal
        /// </summary>
        /// <param name="proposal"></param>
        public void Create(Proposal proposal)
        {
            DbContext.Proposals.Add(proposal);
            DbContext.SaveChanges();
        }

        /// <summary>
        /// This method edits a proposal
        /// </summary>
        /// <param name="project"></param>
        public void Edit(Proposal proposal)
        {
            Proposal original = this.RetrieveProposal(proposal.ProposalID);
            original.CopyValuesFrom(proposal);
            DbContext.SaveChanges();
        }

        /// <summary>
        /// This method retrieve a specific proposal
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public Proposal RetrieveProposal(int proposalID)
        {
            return DbContext.Proposals.Where(p => p.ProposalID == proposalID).First();
        }

        /// <summary>
        /// This method returns all the proposals inside from db
        /// </summary>
        /// <returns></returns>        
        public IQueryable<Proposal> RetrieveProposals()
        {
            return DbContext.Proposals;
        }

        /// <summary>
        /// This method returns all the proposals inside from db
        /// </summary>
        /// <returns></returns>        
        public IQueryable<Proposal> RetrieveProposals(int academicPersonID)
        {            
            return DbContext.Proposals.Where(a => a.AcademicPersonID == academicPersonID);            
        }

        /// <summary>
        /// This method removes a specific proposal from db
        /// </summary>
        /// <param name="subjectID"></param>
        public void Remove(Int32 proposalID)
        {            
            DbContext.Proposals.Remove(RetrieveProposal(proposalID));
            DbContext.SaveChanges();
        }
    }
}
